package com.example.wjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wjh.entity.Announcement;
import com.example.wjh.entity.News;
import com.example.wjh.entity.Result;
import com.example.wjh.mapper.AnnouncementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 教练
 * 后端接口
 * @author
 * @email
 * @date 2022-07-31 07:01:29
 */
@CrossOrigin
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementMapper AnnouncementMapper;

    // 获取最新的三条资讯名称
    @GetMapping("/latest")
    public List<Announcement> getLatestAnnouncements() {
        return AnnouncementMapper.findLatestAnnouncements();
    }

    /**
     * 查询
     * @return
     */
    @GetMapping()
    public Result getAnnouncement(){  return Result.success(AnnouncementMapper.selectList(null));}

    /**
     * 添加
     * @param Announcement
     * @return
     */
    @PostMapping()
    public Result createCoach(@RequestBody Announcement Announcement) {
        AnnouncementMapper.insert(Announcement);
        return Result.success();
    }

    /**
     * 更新
     * @param id
     * @param
     * @return
     */
    @PutMapping("/{id}")
    public Result updateAnnouncement(@PathVariable("id") int id, @RequestBody Announcement Announcement) {
        Announcement existingAnnouncement = AnnouncementMapper.selectById(id);
        if (existingAnnouncement != null) {
            Announcement.setId(id);
            AnnouncementMapper.updateById(Announcement);
            return Result.success();
        } else {
            return null;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delAnnouncement(@PathVariable("id") int id) {
        AnnouncementMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    public Result delAnnouncementBatch(@RequestBody List<Long> ids) {
        AnnouncementMapper.deleteBatchIds(ids);
        return Result.success();
    }

    /**
     * 模糊查询
     * @param
     * @return
     */
    @GetMapping("/search")
    public Result searchAnnouncement(
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String publishtime,
            @RequestParam(required = false) String content
    ) {
        return Result.success(AnnouncementMapper.searchAnnouncement(title,publishtime,content));
    }

    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findByPage")
    public Result getAnnouncementList(@RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize) {
        Page<Announcement> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id"); // 根据id字段降序排序
        page.addOrder(OrderItem.desc("id")); // 添加降序排序条件
        IPage ipage = AnnouncementMapper.selectPage(page,null);
        return Result.success(ipage);
    }
}

